Thrift 简单例子

个人博客原文:Thrift 简单例子

摘要:本文简单的举了个例子,怎么用Thrift去实现一个RPC调用。

编写IDL接口

HelloService.thrift

1
2
3
4
5
6
7
8
namespace java com.thrift.demo.service 

service HelloService {
i32 sayInt(1:i32 param)
string sayString(1:string param)
bool sayBoolean(1:bool param)
void sayVoid()
}

用Thrift编译器编译成对应的类

运行下面命令,生成HelloService.java类

1
thrift-0.10.0 -gen java HelloService.thrift

生成的HelloService.java内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.thrift.demo01.service;

@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-02-13")
public class HelloService {

public interface Iface {

public int sayInt(int param) throws org.apache.thrift.TException;

public java.lang.String sayString(java.lang.String param) throws org.apache.thrift.TException;

public boolean sayBoolean(boolean param) throws org.apache.thrift.TException;

public void sayVoid() throws org.apache.thrift.TException;

}

//......省略了很多代码
}

编写真正业务逻辑实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class HelloServiceImpl implements HelloService.Iface {

public int sayInt(int param) throws TException {
System.out.println("say int :" + param);
return param;
}

public String sayString(String param) throws TException {
System.out.println("say string :" + param);
return param;
}

public boolean sayBoolean(boolean param) throws TException {
System.out.println("say boolean :" + param);
return param;
}
public void sayVoid() throws TException {
System.out.println("say void ...");
}
}

编写Server端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ThriftServer {
public static void main(String[] args) {
try {
// 设置服务器端口
TServerSocket serverTransport = new TServerSocket(9090);
// 设置二进制协议工厂
Factory protocolFactory = new TBinaryProtocol.Factory();
// 处理器关联业务实现
Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>(
new HelloServiceImpl());
//使用单线程标准阻塞I/O模型
TServer.Args simpleArgs = new TServer.Args(serverTransport)
.processor(processor)
.protocolFactory(protocolFactory);
TServer server = new TSimpleServer(simpleArgs);
System.out.println("开启thrift服务器,监听端口:9090");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}

编写Client端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class ThriftClient {
public static void main(String[] args) {
try {
// 设置调用的服务地址-端口
TTransport transport = new TSocket("localhost", 9090);
// 使用二进制协议
TProtocol protocol = new TBinaryProtocol(transport);
// 使用的接口
HelloService.Client client = new HelloService.Client(protocol);
// 打开socket
transport.open();
client.sayBoolean(true);
client.sayString("Hello world");
client.sayInt(20141111);
client.sayVoid();
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException te) {
te.printStackTrace();
}
}
}

运行结果

先运行Server端,再运行Client端
服务端输出:

开启thrift服务器,监听端口:9090
say boolean :true
say string :Hello world
say int :20141111
say void …

到这里,就实现了一个Thrift的RPC调用例子